﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using Clipper.Model;
using System.ComponentModel;

namespace ClipperBaseUI.Chart
{
    /// <summary>
    /// 存储数据点和序列特性。
    /// </summary>
    public  class Series:Control
    {
        #region 变量

        ObservableCollection<Point> mDataPoint;//数据点
        ObservableCollection<Point> mLinePoint;//画线的点
        double mXValueScale;
        double mYValueScale;
        double mXLogBase;
        double mYLogBase;
        bool isShowMark = true;
        bool isShowLine = true;

        #endregion

        #region 属性
        /// <summary>
        /// 获取或设置所属的ChartArea
        /// </summary>
        public ChartArea ChartArea
        {
            get;
            set;
        }
        /// <summary>
        /// 获取或设置ChartArea的名字
        /// </summary>
        public string ChartAreaName
        {
            get;
            set;
        }

        /// <summary>
        /// 是否显示点
        /// </summary>
        [DefaultValue(true)]
        public bool IsShowMark
        {
            get
            {
                return isShowMark;
            }
            set
            {
                isShowMark = value;
            }
        }
        /// <summary>
        /// 显示线
        /// </summary>
        public bool IsShowLine
        {
            get
            {
                return isShowLine;
            }
            set
            {
                isShowLine = value;
            }
        }
        /// <summary>
        /// 曲线的点数据
        /// </summary>
        public ObservableCollection<Point> DataPoint
        {
            get
            {
                if (mDataPoint == null)
                {
                    mDataPoint = new ObservableCollection<Point>();
                }
                return mDataPoint;
            }
            set
            {
                mDataPoint = value;
            }
        }
        /// <summary>
        /// 画线的数据
        /// </summary>
        public ObservableCollection<Point> LinePoint
        {
            get
            {
                if (mLinePoint == null)
                    mLinePoint = new ObservableCollection<Point>();
                return mLinePoint;
            }
            set
            {
                mLinePoint = value;
            }
        }
        #endregion

        public Series()
        {
        }
        /// <summary>
        /// 序列名称
        /// </summary>
        /// <param name="name"></param>
        public Series(string name,ChartArea chartArea)
        {
            Name = name;
            ChartArea = chartArea;
        }
        /// <summary>
        /// 绘制曲线
        /// </summary>
        public void ShowCurve()
        {
            mXValueScale = ChartArea.AxisX.ValueScale;
            mYValueScale = ChartArea.AxisY.ValueScale;
            mXLogBase = ChartArea.AxisX.LogBase;
            mYLogBase = ChartArea.AxisY.LogBase;

            if (mDataPoint != null)
            {
                if (IsShowMark)//显示点
                {
                    ShowData(mDataPoint);
                }
                if (IsShowLine)//显示线
                {
                    ShowLine(mLinePoint);
                }
            }

        }
        public void ShowData(ObservableCollection<Point> points)
        {
            if (points != null)
            {
                foreach (Point point in points)
                {
                    ShowData(point);
                }
            }
        }
        /// <summary>
        /// 绘制曲线,使用数组
        /// </summary>
        /// <param name="value">数组值</param>
        public void ShowData(Array yValue)
        {
            if (yValue != null)
            {
                int i = 1;
                foreach (double value in yValue)
                {
                    Point point = new Point(i, value);
                    ShowData(point);
                    i++;
                }
            }
        }

        /// <summary>
        /// 绘制一个数据点
        /// </summary>
        /// <param name="point"></param>
        public void ShowData(Point point)
        {
            if (mXValueScale == 0 || mYValueScale == 0)
            {
                mXValueScale = ChartArea.AxisX.ValueScale;
                mYValueScale = ChartArea.AxisY.ValueScale;
                mXLogBase = ChartArea.AxisX.LogBase;
                mYLogBase = ChartArea.AxisY.LogBase;
            }

            Ellipse p1 = new Ellipse();
            p1.Height = 2;
            p1.Width = 2;
            p1.MaxHeight = 2;
            p1.MaxWidth = 2;
            p1.Fill = Brushes.Black;
            //位置坐标对齐圆心所以要减去2
            Point pointPosition = DataToPrintPoint(point,1,1);
            Canvas.SetLeft(p1, pointPosition.X);
            Canvas.SetBottom(p1, pointPosition.Y);
            ChartArea.DataArea.Children.Add(p1);
        }

        /// <summary>
        /// 绘制作为线的点
        /// </summary>
        /// <param name="point">数据</param>
        public virtual void ShowLine(ObservableCollection<Point> points)
        {
            
            Point lastPoint = new Point();
            bool lineStatus = false;
            //if (mXValueScale == 0 || mYValueScale == 0)
            //{
                mXValueScale = ChartArea.AxisX.ValueScale;
                mYValueScale = ChartArea.AxisY.ValueScale;
                mXLogBase = ChartArea.AxisX.LogBase;
                mYLogBase = ChartArea.AxisY.LogBase;
            //}
            if (points != null)
            {

                foreach (Point point in points)
                {                    

                    Ellipse p1 = new Ellipse();
                    p1.Height = 5;
                    p1.Width = 5;
                    p1.MaxHeight = 5;
                    p1.MaxWidth = 5;
                    p1.Fill = Brushes.Black;
                    //后面两个参数 位对应的偏移量， 即原点位置                    
                    Point pointPosition = DataToPrintPoint(point, ChartArea.AxisX.MinValue, ChartArea.AxisY.MinValue);                    
                    Canvas.SetLeft(p1, pointPosition.X-2.5);
                    Canvas.SetBottom(p1, pointPosition.Y-2.5);
                    
                    if (lineStatus)
                    {
                        //PathGeometry pg = new PathGeometry();//组合绘制的线段 
                        //Path pa = new Path();//绘制轨迹曲线的容器，用于显示 
                        //pa.Stroke = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                        //pa.StrokeThickness = 1;
                        //PathFigure pf = new PathFigure();
                        //pf.StartPoint = lastPoint;
                        //LineSegment line = new LineSegment();
                        //line.Point = pointPosition;
                        //pf.Segments.Add(line);
                        //pg.Figures.Add(pf);
                        //pa.Data = pg;

                        Line myLine = new Line();
                        myLine.X1 = lastPoint.X;
                        myLine.Y1 =143 - lastPoint.Y;  //line 168 之前的值159
                        myLine.X2 = pointPosition.X;
                        myLine.Y2 = 143 - pointPosition.Y;
                        myLine.Stroke = System.Windows.Media.Brushes.Black;
                        myLine.StrokeThickness = 2;

                        //Canvas.SetLeft(myLine, lastPoint.X);
                        //Canvas.SetBottom(myLine, lastPoint.Y);

                        ChartArea.DataArea.Children.Add(myLine);
                    }

                    lastPoint = pointPosition;
                    lineStatus = true;

                    ChartArea.DataArea.Children.Add(p1);
                    
                }
                //var context = Canvas.gett("2d");
                //context.moveTo(100, 100);
                //context.lineTo(700, 700);
                //context.stroke();
                
            }
        }



        /// <summary>
        /// 计算点的位置
        /// </summary>
        /// <param name="point">点数据</param>
        /// <param name="correctX">X位移</param>
        /// <param name="correctY">Y位移</param>
        /// <returns>位置</returns>
        protected Point DataToPrintPoint( Point point,double correctX,double correctY)
        {
            Point pointPosition = new Point();
            switch (ChartArea.CoordinateType)//不同坐标系中显示方法
            {
                case CoordinateInfoEnum.XY:
                    pointPosition.X = point.X * mXValueScale - correctX* mXValueScale;
                    pointPosition.Y = point.Y * mYValueScale - correctY* mYValueScale;
                    break;
                case CoordinateInfoEnum.LogXLogY:
                    pointPosition.X = Math.Log(point.X, mXLogBase) * mXValueScale - Math.Log(correctX, mXLogBase) * mXValueScale;
                    pointPosition.Y = Math.Log(point.Y, mYLogBase) * mYValueScale - Math.Log(correctY, mXLogBase) * mYValueScale;
                    break;
                case CoordinateInfoEnum.LogXY:
                    pointPosition.X = Math.Log(point.X, mXLogBase) * mXValueScale - correctX;
                    pointPosition.Y = point.Y * mYValueScale - correctY;
                    break;
                case CoordinateInfoEnum.XLogY:
                    pointPosition.X = point.X * mXValueScale - correctX;
                    pointPosition.Y = Math.Log(point.Y, mYLogBase) * mYValueScale - correctY;
                    break;
                default:
                    throw new NotImplementedException();
            }
            return pointPosition;
        }

    }
}
